package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealNew;
import com.itheima.service.SetmealService;
import com.itheima.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/14 17:10
 * @Desc: 套餐管理
 */
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 查询套餐列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/setmeal/page")
    public R<Page<Setmeal>> listAll(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = setmealService.listAll(page, pageSize, name);

        return setmealPage != null ? R.success(setmealPage) : R.error("未查询到任何信息");
    }

    /**
     * 修改套餐状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/setmeal/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        int i = setmealService.updateSatus(status, ids);

        return i == 1 ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/setmeal")
    public R<String> deleteSetmeal(Long[] ids) {
        int i = setmealService.deleteSetmeal(ids);

        return i == 1 ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 添加套餐
     *
     * @param setmealNew
     * @return
     */
    @PostMapping("/setmeal")
    public R<String> addSetmeal(@RequestBody SetmealNew setmealNew) {
        int i = setmealService.addSetmeal(setmealNew);

        return i == 1 ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @GetMapping("/setmeal/{id}")
    public R<SetmealNew> findSetmealById(@PathVariable Long id) {
        SetmealNew setmealById = setmealService.findSetmealById(id);

        return setmealById != null ? R.success(setmealById) : R.error("请求失败，请稍后再试");
    }

    /**
     * 修改套餐信息
     *
     * @param setmealNew
     * @return
     */
    @PutMapping("/setmeal")
    public R<String> updateSetmealInfo(@RequestBody SetmealNew setmealNew) {
        int i = setmealService.updateSetmealInfo(setmealNew);

        return i == 1 ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 查询在售套餐
     *
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/setmeal/list")
    public R<List<Setmeal>> findSetmeal(Long categoryId, Integer status) {
        List<Setmeal> setmeal = setmealService.findSetmeal(categoryId, status);

        return setmeal != null ? R.success(setmeal) : R.error("请求失败，请稍后再试");
    }

    /**
     * 根据套餐id查询套餐详情
     *
     * @param id
     * @return
     */
    @GetMapping("/setmeal/dish/{id}")
    public R<List<Dish>> findDishBySetmealId(@PathVariable Long id) {
        List<Dish> dishes = setmealService.findDishBySetmealId(id);

        return dishes != null ? R.success(dishes) : R.error("请求失败，请稍后再试");
    }
}
